package com.jzo2o.foundations.handler;

import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author elina
 * @date 2023/7/18 16:05
 */
@Component
@Slf4j
public class SpringCacheSyncHandler {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IRegionService regionService;
    @Autowired
    private HomeService homeService;

    @XxlJob(value = "activeRegionCacheSync")
    public void activeRegionCacheSync() {
        log.info("开始同步已开通服务区域列表缓存");
        // 1. 清理缓存
        String key = RedisConstants.CacheName.JZ_CACHE + "::" + "ACTIVE_REGIONS";
        redisTemplate.delete(key);

        // 2. 刷新缓存
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();
        log.info("已开通服务区域列表缓存同步完成");
        // 遍历区域，将区域下的服务项添加到缓存中
        regionSimpleResDTOS.forEach(
                item -> {
                    String regionKey = RedisConstants.CacheName.SERVE_ICON + "::" + item.getId();
                    redisTemplate.delete(regionKey);
                    homeService.queryServeIconCategoryByRegionIdCache(item.getId());
                    String serveTypeKey = RedisConstants.CacheName.SERVE_TYPE + "::" + item.getId();
                    redisTemplate.delete(serveTypeKey);
                    homeService.queryServeTypeListCache(item.getId());
                }
        );
    }

    /**
     * 每天凌晨缓存服务类型列表
     */
    @XxlJob(value = "cacheServeTypeListJob")
    public void cacheServeTypeList() {
        log.info("开始执行缓存服务类型列表任务");
        // 获取所有启用的区域
        List<RegionSimpleResDTO> activeRegions = regionService.queryActiveRegionListCache();

        if (activeRegions != null && !activeRegions.isEmpty()) {
            for (RegionSimpleResDTO region : activeRegions) {
                // 删除原有缓存
                String serveTypeKey = RedisConstants.CacheName.SERVE_TYPE + "::" + region.getId();
                redisTemplate.delete(serveTypeKey);

                // 重新加载缓存
                homeService.queryServeTypeListCache(region.getId());
                log.info("区域[id={}]服务类型列表缓存完成", region.getId());
            }
        }
        log.info("缓存服务类型列表任务执行完成");
    }

    /**
     * 每天凌晨缓存热门服务列表
     */
    @XxlJob(value = "cacheHotServeListJob")
    public void cacheHotServeList() {
        log.info("开始执行缓存热门服务列表任务");
        // 获取所有启用的区域
        List<RegionSimpleResDTO> activeRegions = regionService.queryActiveRegionListCache();

        if (activeRegions != null && !activeRegions.isEmpty()) {
            for (RegionSimpleResDTO region : activeRegions) {
                // 删除原有缓存
                String hotServeKey = RedisConstants.CacheName.HOT_SERVE + "::" + region.getId();
                redisTemplate.delete(hotServeKey);

                // 重新加载缓存
                homeService.queryHotServeListCache(region.getId());
                log.info("区域[id={}]热门服务列表缓存完成", region.getId());
            }
        }
        log.info("缓存热门服务列表任务执行完成");
    }

    /**
     * 定时任务更新热门服务相关缓存
     * 查询出所有热门服务，遍历热门服务列表先删除缓存，再执行查询方法对热门服务信息进行缓存
     */
    @XxlJob(value = "refreshHotServeRelatedCacheJob")
    public void refreshHotServeRelatedCache() {
        log.info("开始执行热门服务相关信息缓存更新任务");
        // 获取所有启用的区域
        List<RegionSimpleResDTO> activeRegions = regionService.queryActiveRegionListCache();

        if (activeRegions != null && !activeRegions.isEmpty()) {
            for (RegionSimpleResDTO region : activeRegions) {
                // 获取该区域下的热门服务列表（同时会清除缓存）
                List<com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO> hotServes =
                        homeService.queryHotServeListCache(region.getId());

                if (hotServes != null && !hotServes.isEmpty()) {
                    for (com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO serve : hotServes) {
                        // 先删除服务信息缓存
                        String serveKey = RedisConstants.CacheName.SERVE_ITEM + "::" + serve.getId();
                        redisTemplate.delete(serveKey);

                        // 再执行查询方法对热门服务信息进行缓存
                        homeService.queryServeByIdCache(serve.getId());
                        log.info("服务[id={}]信息缓存更新完成", serve.getId());
                    }
                }
            }
        }
        log.info("热门服务相关信息缓存更新任务执行完成");
    }


}
